<script>
import outMain from "@/components/outMain.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { Staffuser, staffCheckStatus, renewNode, getXfmoney } from "@/http/api";
export default {
  components: { outMain },
  name: "",
  data() {
    return {
      showRenew: false,
      year_sum: 1,
      coin_num: 0,
      renewActive: {},
      tableData: [],
      statusArr: [
        { label: "全部", value: "" },
        { label: "正常", value: "1" },
        { label: "禁用", value: "2" },
        { label: "已到期", value: "3" },
      ],
      renewNumArr: [
        { label: "1年", value: 1 },
        { label: "2年", value: 2 },
        { label: "3年", value: 3 },
      ],
      queryData: {
        staff_status: "",
        staff_user_name: "",
        page: 1,
        per_page: 20,
      },
      total: 0,
    };
  },
  props: {},
  setup() {},
  methods: {
    addFn(id) {
      this.$router.push({
        path: "/staffListAdd",
        query: { id: id, page: id ? this.queryData.page : undefined },
      });
    },
    detailFn(row) {
      this.$router.push({
        path: "/staffListDetail",
        query: { id: row.staff_id, page: this.queryData.page },
      });
    },
    questCheck(data) {
      staffCheckStatus(data).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "操作成功",
          });
          this.getData();
        }
      });
    },
    checkStatus(row, type) {
      if (type == 1) {
        ElMessageBox.confirm("确定恢复选择的员工账号吗?", "操作提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then(() => {
          let data = {
            staff_id: row.staff_id,
            staff_status: type,
          };
          this.questCheck(data);
        });
      } else {
        ElMessageBox.prompt("确定禁用选择的员工账号吗?", "操作提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }).then((value) => {
          let data = {
            staff_id: row.staff_id,
            staff_status: type,
            jy_reason: value.value,
          };
          this.questCheck(data);
        });
      }
    },
    getData() {
      Staffuser(this.queryData).then((res) => {
        if (res.data.code == 1) {
          this.tableData = res.data.data.data;
          this.total = res.data.data.total;
        }
      });
    },
    renewFn(row) {
      this.renewActive = row;
      this.showRenew = true;
    },
    sureRenew() {
      renewNode({
        year_sum: this.year_sum,
        staff_id: this.renewActive.staff_id,
      }).then((res) => {
        if (res.data.code == 1) {
          ElMessage({
            type: "success",
            message: "续费成功",
          });
          this.showRenew = false;
          this.getData();
        }
      });
    },
    getStaffCoin() {
      getXfmoney().then((res) => {
        if (res.data.code == 1) {
          this.coin_num = res.data.data;
        }
      });
    },
  },
  mounted() {},
  created() {
    this.queryData.page = Number(localStorage.getItem("boot_page")) || 1;
    this.getData();
    this.getStaffCoin();
  },
};
</script>
<template>
  <outMain>
    <div class="tableCont">
      <div class="tableName flexCont">
        <div>员工管理（{{ total }}）</div>
      </div>
      <div class="tableSerch">
        <div class="serchLeft">
          <btn value="新增" @ok="addFn"></btn>
        </div>
        <div class="serchRight">
          <el-form class="flexCenter" :inline="true">
            <el-form-item>
              <el-input
                clearable
                @clear="getData"
                @keyup.enter="getData"
                v-model="queryData.staff_user_name"
                placeholder="请输入员工姓名"
              />
            </el-form-item>
            <el-form-item>
              <el-select
                v-model="queryData.staff_status"
                clearable
                @change="getData"
                placeholder="请选择账号状态"
              >
                <el-option
                  v-for="item in statusArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
            <btn value="搜索" @click="getData"></btn>
          </el-form>
        </div>
      </div>
      <el-table class="tableMain" :data="tableData" stripe style="width: 100%">
        <el-table-column prop="staff_number" width="100" label="编号" />
        <el-table-column
          prop="staff_user_name"
          width="120"
          show-overflow-tooltip
          label="员工姓名"
        />
        <el-table-column
          prop="staff_user_mobile"
          width="160"
          show-overflow-tooltip
          label="手机号"
        />
        <el-table-column
          prop="staff"
          width="120"
          show-overflow-tooltip
          label="账号状态"
        >
          <template #default="{ row }">
            <div class="tagGroup">
              <el-tag type="success" v-if="row.staff_status == 1">正常</el-tag>
              <el-tag type="danger" v-if="row.staff_status == 2">禁用</el-tag>
              <el-tag type="info" v-if="row.staff_status == 3">已到期</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          width="140"
          prop="expiration_time"
          show-overflow-tooltip
          label="到期时间"
        />
        <el-table-column
          prop="lastlogin_time"
          show-overflow-tooltip
          label="最近登录"
        />
        <el-table-column
          prop="create_time"
          show-overflow-tooltip
          label="创建时间"
        />

        <el-table-column prop="Name" width="180" label="操作">
          <template #default="{ row }">
            <div class="tableCtrl">
              <div class="greenText ctrlText" @click="detailFn(row)">详情</div>
              <div class="blueText ctrlText" @click="addFn(row.staff_id)">
                编辑
              </div>
              <div
                class="blueText ctrlText"
                v-if="row.staff_status == 1 || row.staff_status == 3"
                @click="renewFn(row)"
              >
                续费
              </div>
              <div
                class="blueText ctrlText"
                @click="checkStatus(row, 1)"
                v-if="row.staff_status === 2"
              >
                恢复
              </div>
              <div
                class="redText ctrlText"
                @click="checkStatus(row, 2)"
                v-if="row.staff_status === 1"
              >
                禁用
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pageCont">
        <el-pagination
          v-model:currentPage="queryData.page"
          v-model:page-size="queryData.per_page"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
          @size-change="getData"
          @current-change="getData"
        />
      </div>
      <el-dialog v-model="showRenew" title="账号续费" width="500">
        <el-form>
          <el-form-item label-width="120" label="续费账号">
            <div>
              {{ renewActive.staff_user_name }}({{ renewActive.staff_number }})
            </div>
          </el-form-item>
          <el-form-item label-width="120" label="续费时间">
            <div style="width: 100%">
              <el-select v-model="year_sum" placeholder="请选择续费时间">
                <el-option
                  v-for="item in renewNumArr"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </div>
          </el-form-item>
          <el-form-item label-width="120" label="所需鲸币">
            <div>{{ Number(coin_num) * Number(year_sum) }}鲸币</div>
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="showRenew = false">取消</el-button>
            <el-button type="primary" @click="sureRenew"> 确认续费 </el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </outMain>
</template>
<style scoped lang="less"></style>
